package light.head.dao.site;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import light.head.cache.Cache;
import light.head.framework.base.BaseDAO;
import light.head.model.WorkGuide;

import org.springframework.stereotype.Service;

@Service
public class WorkGuideDAO extends BaseDAO<WorkGuide, Serializable>{
	private static final String sql_loadAll = "FROM WorkGuide wg WHERE 1 = 1 AND wg.deleted = 0";
	//private static final String sql_getSonList = "FROM WorkGuide wg WHERE wg.deleted = 0  AND wg.pc = ?";
	private static final String sql_indexlist = "SELECT wg.code, wg.name FROM WorkGuide wg  WHERE wg.deleted = 0 AND wg.pc = 0 LIMIT 18";
	private static final String sql_getAllList = "FROM WorkGuide wg  WHERE wg.deleted = 0 AND wg.pc = 0";
	private static final String sql_getByName = "FROM WorkGuide wg  WHERE wg.deleted = 0 AND wg.pc = 0 AND wg.name LIKE ?";
	private static final String sql_getListByPc = "FROM WorkGuide wg  WHERE wg.deleted = 0 AND wg.pc = ?";
	public List<WorkGuide> getAll() {
		 List<WorkGuide> list = Cache.workGuideList;
		 if (null == list) {
			 list = loadAll();
		 }
		 return list;
	 }
	
	// 查询所有类型，并保存其父子级关系
	 public List<WorkGuide> loadAll() {
		 List<WorkGuide> list = find(sql_loadAll);
		 if (null != list && list.size() > 0) {
			 for (WorkGuide wg : list) {
				 for (WorkGuide son: list) {
					 if (null == wg.getSonList())
					 wg.setSonList(new ArrayList<WorkGuide>());
						 
					 if (wg.getCode() == son.getPc())
						 wg.getSonList().add(son);
				 }
			 }
				 
		 List<WorkGuide> slist = new ArrayList<WorkGuide>();
		 	for (WorkGuide wg : list) {
				if (null == wg.getPc() || wg.getPc() == 0) {
					slist.add(wg);
				}
		 	}
			 Cache.workGuideList = slist;
				return slist;
		 }
		 Cache.workGuideList = null;
		 return null;
	 }
	 
	/*//获取子类列表
	private List<WorkGuide> getSonList(Integer pc){
		return super.findI(sql_getSonList, pc);
	}*/
	
	//获取首页List
	public List<WorkGuide> getIndexList(){
		return query(sql_indexlist,null,null);
	}
	//获取所有LIST
	public List<WorkGuide> getAllList(){
		return super.find(sql_getAllList);
	}
	
	//用name 模糊查询 
	public List<WorkGuide> getByName(String name){
		return super.find(sql_getByName,"%" + name + "%");
	}
	
	//通过pc查询
	public List<WorkGuide> getByPc(Integer pc){
		return super.find(sql_getListByPc,pc);
	}
}
